﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SDLITS.Model;
using FineUI;

namespace SDLITS.Common
{
    public class CommonBase:System.Web.UI.Page
    {
        private bool InitSession(V_LoginUser ds)
        {
            try
            {
                ModelLoginUser modelLoginUser = new ModelLoginUser
                {
                    PositionID = ds.PositionID.HasValue ? ds.PositionID.Value : -1,
                    RoomID = ds.RoomID.HasValue ? Convert.ToInt32(ds.RoomID) : -1,
                    CompanyName = string.IsNullOrEmpty(ds.CompanyName) ? "" : ds.CompanyName,
                    CompanyID = ds.CompanyID.HasValue ? Convert.ToInt32(ds.CompanyID) : -1,
                    DepartmentName = string.IsNullOrEmpty(ds.DeptName) ? "" : ds.DeptName,
                    DeptID = ds.DeptID.HasValue ? Convert.ToInt32(ds.DeptID) : -1,
                    MemCardNo = string.IsNullOrEmpty(ds.MemCardNo) ? "" : ds.MemCardNo,
                    MemCardID = ds.MemCardID.HasValue ? Convert.ToInt32(ds.MemCardID) : -1,
                    RoleID = ds.RoleID.HasValue ? Convert.ToInt32(ds.RoleID) : -1,
                    UserID = ds.UserID,
                    LoginTime = ds.LoginTime.HasValue ? ds.LoginTime.ToString() : "",
                    RealName = string.IsNullOrEmpty(ds.RealName) ? "" : ds.RealName,
                    UserName = string.IsNullOrEmpty(ds.UserName) ? "" : ds.UserName,
                };
                Session["LoginUser"] = modelLoginUser;
                Session["IsLogin"] = "True";
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            if (Session["LoginUser"] == null || Session["IsLogin"] == null || Session["IsLogin"].ToString() != "True")
            {
                //V_LoginUser user = SDDbSession.Default.From<V_LoginUser>().Where(V_LoginUser._.UserName == "001" && V_LoginUser._.UserPwd == "111111").ToFirst();
                //InitSession(user);

                Response.Write("<script language='javascript'>alert('超时登录！');top.location.href ='/login.aspx';</script>");
                Response.End();
            }
            
            if (!IsPostBack)
            {
                #region 更换页面皮肤
                if (PageManager.Instance != null)
                {
                    HttpCookie themeCookie = Request.Cookies["theme_v4"];
                    if (themeCookie != null)
                    {
                        string themeValue = themeCookie.Value;

                        if (IsSystemTheme(themeValue))
                        {
                            PageManager.Instance.Theme = (Theme)Enum.Parse(typeof(Theme), themeValue, true);
                        }
                    }
                }
                #endregion
            }
            base.OnInit(e);
        }
        private bool IsSystemTheme(string themeName)
        {
            string[] themes = Enum.GetNames(typeof(Theme));
            foreach (string theme in themes)
            {
                if (theme.ToLower() == themeName)
                {
                    return true;
                }
            }
            return false;
        }
    }
}